package com.yw.atp.service;

import java.util.LinkedList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.yw.atp.model.Command;
import com.yw.atp.model.Reply;
import com.yw.atp.model.Staff;
import com.yw.atp.util.StringUtil;


@Repository
@Transactional
public class ReplyService{
	@PersistenceContext
    private EntityManager em;
	public List<Reply> getReplys(Reply reply){
		if(reply.getCommand()!=null&&StringUtil.checkID(reply.getCommand().getId())){
			List<Staff> staffs = getStaffs(reply.getCommand());
			if(staffs!=null&&staffs.size()>0){
				List<Reply> res = new LinkedList<Reply>();
				List<Reply> resA = new LinkedList<Reply>();
				List<Reply> resB = new LinkedList<Reply>();
				for (Staff staff:staffs){
					Reply rree = getTheReplysOfStaff(reply.getCommand(),staff);
					if(rree!=null&&StringUtil.checkID(rree.getId())){
						resA.add(rree);
					}else{
						Reply re = new Reply();
						re.setOwner(staff);
						re.setCommand(reply.getCommand());
						re.setContent("-未读-");
						resB.add(re);
					}
				}
				res.addAll(resA);
				res.addAll(resB);
				return res;
			}
			return null;
		}else{
			StringBuffer sb = new StringBuffer();
			sb.append("select r FROM Reply r where r.status !='1' ");
			if(reply.getOwner()!=null&&reply.getOwner().getId()!=null&&reply.getOwner().getId().intValue()>0){
				sb.append(" and r.owner.id = :staffID ");
			}
			if(reply.getCommand()!=null&&reply.getCommand().getId()!=null&&reply.getCommand().getId().intValue()>0){
				sb.append(" and r.command.id = :commID ");
			}
			Query query = em.createQuery(sb.toString());
			int staffID   = reply.getOwner()!=null?(reply.getOwner().getId()>0?reply.getOwner().getId():0):0;
			int commID    = reply.getCommand()!=null?(reply.getCommand().getId()>0?reply.getCommand().getId():0):0;
			if(reply.getOwner()!=null&&reply.getOwner().getId()!=null&&reply.getOwner().getId().intValue()>0){
				query.setParameter("staffID",staffID);
			}
			if(reply.getCommand()!=null&&reply.getCommand().getId()!=null&&reply.getCommand().getId().intValue()>0){
				query.setParameter("commID",commID);
			}
			return (List<Reply>)query.getResultList();
		}
	}
	
	
	
	public List<Staff> getStaffs(Command command){
		Query query = em.createQuery("select s FROM  Staff s,Command c where (s in elements(c.staffList) or s.department in elements(c.deparList) or exists (select p from Profession p where p in  elements(s.professions) and p in elements(c.proList))) and c.id = :commID order by s.id desc");
		query.setParameter("commID",command.getId());
		return (List<Staff>)query.getResultList();
	}
	
	public List<Reply> getPollingReplys(Integer maxID,String ids,Staff staff){
		StringBuffer sb = new StringBuffer();
		sb.append("select r FROM Reply r where r.owner.id != :ownerID and r.id >:maxID and r.id not in ("+ids+") and r.command.sender.id = :sendID and  r.status !='1' order by r.id desc");		
		Query query = em.createQuery(sb.toString());
		query.setParameter("ownerID",staff.getId());
		query.setParameter("sendID",staff.getId());
		query.setParameter("maxID",maxID);
		return (List<Reply>)query.getResultList();
	}
	
	public List<Reply> loadReplys(int id,Staff staff){
		StringBuffer sb = new StringBuffer();
		sb.append("select r FROM Reply r where r.owner.id != :ownerID and r.command.id = :commID order by r.id desc");	
		Query query = em.createQuery(sb.toString());
		query.setParameter("ownerID",staff.getId());
		query.setParameter("commID",id);
		return (List<Reply>)query.getResultList();
	}
	
	public String getValue(String str){
		if(str==null){
			return "";
		}
		return str;
	}
	
	public Reply getTheReplysOfStaff(Command c,Staff s){
		StringBuffer sb = new StringBuffer();
		sb.append("select r FROM Reply r where r.owner.id = :ownerID and r.command.id = :commID order by r.id desc");	
		Query query = em.createQuery(sb.toString());
		query.setParameter("ownerID",s.getId());
		query.setParameter("commID",c.getId());
		List<Reply> list = (List<Reply>)query.getResultList();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
}
